﻿using System;
using System.Collections.Generic;

namespace Douyu.SDK
{
    /// <summary>
    /// 
    /// </summary>
    public class SafeDictionary
    {
        public Dictionary<string, object> Datas { get; protected set; }

        public SafeDictionary()
        {
            this.Datas = new Dictionary<string, object>();
        }

        public SafeDictionary(Dictionary<string, object> datas)
        {
            this.Datas = datas;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        protected void SetValue(string key, object value)
        {
            if (this.Datas.ContainsKey(key))
            {
                this.Datas[key] = value;
            }
            else
            {
                this.Datas.Add(key, value);
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        protected T GetValue<T>(string key, T defaultValue)
        {
            if (this.Datas.ContainsKey(key))
            {
                var type = typeof(T);
                return (T)Convert.ChangeType(this.Datas[key], type);
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 消息类别名
        /// </summary>
        public string type
        {
            get { return GetValue(nameof(type), ""); }
            set { SetValue(nameof(type), value); }
        }

    }

}
